﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//.............
using QLKTX1._0.DTO;
using QLKTX1._0.DB;
using System.Data.SqlClient;
using System.Data;
//...................
namespace QLKTX1._0.DAO
{
    class NamHocDAO
    {
        //Ham Lay danh sach cac doi tuong cua bang namHoc
        public static List<NamHocDTO> dsNamHoc()
        {
            List<NamHocDTO> dsNamHoc = new List<NamHocDTO>();

            ConnectDB con = new ConnectDB();

            DataTable dt = con.getDataTable("SELECT * FROM namHoc");

            for (int i=0; i<dt.Rows.Count;i++ )
            {
                NamHocDTO namHoc = new NamHocDTO();
                namHoc.MaNamHoc = Int32.Parse(dt.Rows[i]["maNamHoc"].ToString());
                namHoc.TenNamHoc = dt.Rows[i]["tenNamHoc"].ToString();
                dsNamHoc.Add(namHoc);

            }
            return dsNamHoc;
        }
        public static List<NamHocDTO> filter()
        {
            List<NamHocDTO> dsNamHoc = new List<NamHocDTO>();

            ConnectDB con = new ConnectDB();

            NamHocDTO n = new NamHocDTO();
            n.MaNamHoc = 0;
            n.TenNamHoc = "Tất cả";
            dsNamHoc.Add(n);

            DataTable dt = con.getDataTable("SELECT * FROM namHoc");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NamHocDTO namHoc = new NamHocDTO();
                namHoc.MaNamHoc = Int32.Parse(dt.Rows[i]["maNamHoc"].ToString());
                namHoc.TenNamHoc = dt.Rows[i]["tenNamHoc"].ToString();
                dsNamHoc.Add(namHoc);

            }
            return dsNamHoc;
        }
        //Ham Insert
        public static bool themNamHoc(NamHocDTO namHoc)
        {

            ConnectDB con = new ConnectDB();// dung DB

            return con.executeQuery("INSERT INTO namHoc(tenNamHoc) VALUES('" + namHoc.TenNamHoc + "')");

        }
        //Ham Delete
        public static bool xoaNamHoc(NamHocDTO namHoc)
        {
            ConnectDB con = new ConnectDB();

            return con.executeQuery("DELETE FROM namHoc WHERE maNamHoc=" + namHoc .MaNamHoc  + "");
        }
        //Ham Update
        public static bool capNhatNamHoc(NamHocDTO namHoc)
        {
            ConnectDB con = new ConnectDB();

            return con.executeQuery("UPDATE namHoc set tenNamHoc='" + namHoc.TenNamHoc + "' WHERE maNamHoc='" + namHoc.MaNamHoc + "'");
        }
    }
}
